package com.example.demo.pdf;

import com.example.demo.pdfbox.PdfGenerator;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.*;
import org.apache.pdfbox.util.Matrix;

import java.io.IOException;
import java.io.InputStream;

public class Pdf {
    public static void main(String[] args) throws IOException {
        PDDocument doc = new PDDocument();
        PDPage page1 = new PDPage();
        doc.addPage(page1);
        PDPage page2 = new PDPage();
        doc.addPage(page2);
        PDPage page3 = new PDPage();
        doc.addPage(page3);
        PDPage page4 = new PDPage();
        doc.addPage(page4);
        InputStream inNFont = PdfGenerator.class.getClassLoader().getResourceAsStream( "font/AlibabaSans-Regular.ttf");

        PDType0Font font = PDType0Font.load(doc, inNFont);
        float fontSize = 20;
        int xPos = 100;
        int yPos = 100;
        float titleWidth = font.getStringWidth("Tets") / 1000;
        float titleHeight = fontSize;
        float tx = titleWidth / 2;
        float ty = titleHeight / 2;

        try (PDPageContentStream contentStream = new PDPageContentStream(doc, page1)) {
            contentStream.beginText();
            contentStream.setFont(font, fontSize);

            contentStream.newLineAtOffset(xPos, yPos);
            contentStream.showText("Tets1");
            Matrix translateInstance = Matrix.getTranslateInstance(100, 200);
            translateInstance.translate(100, 100);
            contentStream.setTextMatrix(translateInstance);

            contentStream.showText("Tets2");
            contentStream.endText();
        }

        // classic case of rotated page
        try (PDPageContentStream contentStream = new PDPageContentStream(doc, page2)) {
            contentStream.beginText();
            contentStream.setFont(font, fontSize);

            Matrix matrix = Matrix.getRotateInstance(Math.toRadians(90), 200, 100);
            // matrix.translate(0, -page2.getMediaBox().getWidth());

            contentStream.setTextMatrix(matrix);


            contentStream.showText("Tet11111111111111111111s1");
            Matrix matrix1 = Matrix.getRotateInstance(Math.toRadians(30), 100, 100);
            contentStream.setTextMatrix(matrix1);

            // contentStream.newLineAtOffset(xPos, yPos);
            contentStream.showText("Tet1111111111111111111111s2");
            contentStream.endText();
        }

        // rotation around text
        try (PDPageContentStream contentStream = new PDPageContentStream(doc, page3)) {
            contentStream.beginText();
            Matrix scaleInstance = Matrix.getScaleInstance(0.5F, 0.5f);
            //
            contentStream.setTextMatrix(scaleInstance);

            contentStream.newLineAtOffset(100, 100);

            contentStream.setFont(font, fontSize);
            contentStream.showText("Tets");
            contentStream.endText();
        }

        // rotation around text
        try (PDPageContentStream contentStream = new PDPageContentStream(doc, page4)) {
            contentStream.beginText();
            Matrix matrix = new Matrix(1, 0f, (float) Math.toRadians(10), 1, 100, 100);
            //
            contentStream.setTextMatrix(matrix);

            contentStream.setFont(font, fontSize);
            // contentStream.newLineAtOffset(100, 100);
            contentStream.showText("AdfasdfLKJLKHalkwejf635416");
            // contentStream.newLineAtOffset(100, 300);
            matrix = new Matrix(1, 0f, (float) Math.toRadians(10), 1, 100, 300);
            contentStream.setTextMatrix(matrix);
            contentStream.showText("AdfasdfLKJLKHalkwejf635416");

        }
        doc.save("D:\\mypdf.pdf");
        doc.close();
    }


}
